package cn.org.donald.framework.controller;

import cn.org.donald.framework.constant.InfoConstant;
import cn.org.donald.framework.constant.TableConstant;
import cn.org.donald.framework.pojo.dto.AddUserRoleDto;
import cn.org.donald.framework.pojo.vo.HttpResult;
import cn.org.donald.framework.service.SysRoleService;
import cn.org.donald.framework.service.SysUserService;
import cn.org.donald.pojo.entity.system.Role;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.org.donald.framework.base.BaseController;
import cn.org.donald.framework.pojo.dto.PageDto;
import cn.org.donald.framework.pojo.vo.TableResult;
import cn.org.donald.pojo.entity.system.UserRole;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ： Donald
 * @date ： 2020/11/7 21:18
 * @description：
 */
@RestController
@Api(value = "角色管理",description = "角色管理相关接口")
@RequestMapping("/sysRole")
public class SysRoleController extends BaseController {

    @Reference(version = "1.0.0",group = "donald")
    private SysRoleService sysRoleService;

    @Reference(version = "1.0.0",group = "donald")
    private SysUserService sysUserService;

    @PostMapping("/getList")
    @ApiOperation(value = "查询全集合")
    @RequiresPermissions("sysRole:getList:post")
    public TableResult getList(@RequestBody(required = false) Role role){
        List<Role> list = sysRoleService.list(new QueryWrapper<>(role));
        return toTableResult(list);
    }

    @PostMapping("/getPage")
    @ApiOperation(value = "角色分页查询")
    @RequiresPermissions("sysRole:getPage:post")
    public TableResult getPage(@RequestBody PageDto<Role> pageDto){
        IPage<Role> roleIPage = sysRoleService.queryListPage(pageDto);
        return toTableResult(roleIPage);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除角色")
    @RequiresPermissions("sysRole:delete:delete")
    public HttpResult delete(@PathVariable Long id){
        if (sysRoleService.isAdmin(id)){
            return toResultFail(InfoConstant.ADMIN_ROLE_EXCEPT_INFO);
        }
        int rows = sysRoleService.deleteById(id);
        return rows>0?toResultSuccess(InfoConstant.DELETE_SUCCESS):toResultFail(InfoConstant.DELETE_FAIL);
    }

    @PutMapping("/edit")
    @ApiOperation(value = "编辑角色")
    @RequiresPermissions("sysRole:edit:put")
    public HttpResult edit(@RequestBody Role role){
        if (sysRoleService.isAdmin(role.getId()))return toResultFail(InfoConstant.ADMIN_ROLE_EXCEPT_INFO);
        sysRoleService.updateById(role);
        return toResultSuccess(InfoConstant.EDIT_SUCCESS);
    }

    @GetMapping("/getByUserId")
    @ApiOperation(value = "获取用户已拥有的角色")
    @RequiresPermissions("sysRole:getByUserId:get")
    public TableResult getByUserId(Long id){
        List<Role> roleList = sysRoleService.findByUserId(id);
        return toTableResult(roleList);
    }

    @GetMapping("/getByNotUserId")
    @ApiOperation(value = "获取用户未拥有的角色")
    @RequiresPermissions("sysRole:getByNotUserId:get")
    public TableResult getByNotUserId(Long id){
        List<Role> roleList = sysRoleService.findByNotUserId(id);
        return toTableResult(roleList);
    }

    @PostMapping("/addUserRole")
    @ApiOperation(value = "用户角色批量分配")
    @RequiresPermissions("sysRole:addUserRole:post")
    public HttpResult addUserRole(@RequestBody AddUserRoleDto addUserRoleDto){
        if (addUserRoleDto.getUserName().equals(TableConstant.VALUE_ADMIN_USER_NAME)){
            return toResultFail(InfoConstant.ADMIN_USER_EXCEPT_INFO);
        }
        List<Role> listDto = addUserRoleDto.getRoleList();
        if (listDto.isEmpty()||listDto.get(0).getId()==null){
            return toResultFail("没有可添加分配角色");
        }
        int rows = sysRoleService.insertUserRoleBatch(addUserRoleDto);
        return rows>0? toResultSuccess("添加分配成功"):toResultFail("添加分配失败");
    }

    @PostMapping("/deleteUserRole")
    @ApiOperation(value = "移除用户已分配的权限")
    @RequiresPermissions("sysRole:deleteUserRole:post")
    public HttpResult deleteUserRole(@RequestBody UserRole userRole){

        if (sysUserService.isAdminUser(userRole.getUserId())){
            return toResultFail(InfoConstant.ADMIN_USER_EXCEPT_INFO);
        }
        if (userRole.getUserId()==null||userRole.getRoleId()==null){
            return toResultFail("没有可移除的角色");
        }
        int rows = sysRoleService.deleteUserRole(userRole);
        return rows>0? toResultSuccess(InfoConstant.REMOVE_SUCCESS):toResultFail(InfoConstant.REMOVE_FAIL);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增角色")
    @RequiresPermissions("sysRole:add:post")
    public HttpResult add(@RequestBody Role role){
        return sysRoleService.save(role)? toResultSuccess(InfoConstant.ADD_SUCCESS) : toResultFail(InfoConstant.ADD_FAIL);
    }
}
